<template>
  <div class="xtx-form">
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <input
          v-model="form.account"
          class="input"
          type="text"
          placeholder="请输入用户名"
        />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input
          v-model="form.mobile"
          class="input"
          type="text"
          placeholder="请输入手机号"
        />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input
          v-model="form.code"
          class="input"
          type="text"
          placeholder="请输入验证码"
        />
        <span class="code" @click="btnSend">{{
          countTime == 0 ? "发送验证码" : countTime + "s后在获取"
        }}</span>
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input
          v-model="form.password"
          class="input"
          type="password"
          placeholder="请输入密码"
        />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input
          v-model="form.rePassword"
          class="input"
          type="password"
          placeholder="请确认密码"
        />
      </div>
      <div class="error"></div>
    </div>
    <a href="javascript:;" class="submit" @click="submit">立即提交</a>
  </div>
</template>

<script>
import QC from "qc";
import { useRouter } from "vue-router";
import { reactive, ref } from "vue";
import { useCountDown } from "@/compositions";
import { message } from "@/components/Message";
import { reqUserQQPatchCode, reqUserQQPatchLogin } from "@/api/user";

export default {
  name: "CallbackPatch",
  props: {
    openId: {
      type: String,
      default: "",
    },
  },
  setup(props) {
    const router = useRouter();
    const form = reactive({
      mobile: "13297556677",
      code: "123456",
      account: Date.now(),
      password: "123456",
      rePassword: "123456",
    });
    const { countTime, start, pause, resume } = useCountDown(60);
    const btnSend = async () => {
      // 防止连续点击
      if (countTime.value > 0) return;
      /*
      1-校验手机号
      2-发送请求获取验证码
      */
      var reg =
        /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
      if (!reg.test(form.mobile)) {
        message({ type: "error", text: "请输入合法的11位手机号" });
        return;
      }

      const res = await reqUserQQPatchCode(form.mobile).catch((err) => {
        console.dir(err.response, 88);
        return message({ type: "error", text: err.response.data.message });
      });
      message({ type: "success", text: "手机号已发送" });
      start(); //  开启倒计时
    };

    const submit = async () => {
      //1-验证表单
      //2-发送请求
      var reg =
        /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
      if (!reg.test(form.mobile)) {
        message({ type: "error", text: "请输入合法的11位手机号" });
        return;
      }
      if (!form.code.trim()) {
        message({ type: "error", text: "请输入验证码" });
        return;
      }

      const res = await reqUserQQPatchLogin({ ...form, unionId: props.openId });

      // router.push("/");
    };

    return { btnSend, countTime, form, submit };
  },
};
</script>

<style scoped lang="less">
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
